let app = new Vue({
	el: "#app",
	data() {
		return {
			testid:'',
			showrightchoose: false,
			testTime:3,
			radio:'1',
			results:[],
			hidetimu:false,
			resetBtn: false,
			resetonBtn: false,
			submitDisable: true,
			nextDisable: false,
			beforeDisable: true,
			answerDisable: false,
			comfirm: false,
			show_right_answer: false,
			currentPage: 1, //当前第几页
			pageSize: 20, //每页显示几条数据
			number: 0,
			right: 0,
			onegoal: 5,
			pageData: [], //单页数据
			Form: [],
			achievementForm: {
				testTotalTimes:"",
				seriesId:"",
				examId:"",
				testId: "",
				score: "",
				name: "",
				identityCard: "",
				totalScore: "",
				passMark: "",
				testNumber: "",
				testType: "",
				stestType:"",
				questionTotal: "",
				achievement: [],
			},
			testpaperForm: {
				testId: "",
				examId:"",
				identityCard: '',
				name: '',
				questionTotal: '',
				totalScore: '',
				passMark: '',
				testNumber: "",
				danxuan:0,
				duoxuan:0,
				testType: '',
				stestType: ''
			},
		}
	},
	methods: {
		init() {
			var $this = this;
			this.pageData = this.achievementForm.achievement.slice((this.currentPage - 1) * this.pageSize, this.currentPage *
				this.pageSize);
			this.pageData.forEach(function(item, i) {
				if (item.type == "单选") {
					item.picked_radio = $this.$cookies.get('pickedd_' + (($this.currentPage - 1) * $this.pageSize + i));

				} else if (item.type == "多选") {
					if (JSON.parse($this.$cookies.get('pickedd_' + (($this.currentPage - 1) * $this.pageSize + i))) == null ||
						JSON.parse($this.$cookies.get('pickedd_' + (($this.currentPage - 1) * $this.pageSize + i))) == undefined) {
						item.pickedMany = [];
					} else {
						item.pickedMany = JSON.parse($this.$cookies.get('pickedd_' + (($this.currentPage - 1) * $this.pageSize + i)));
					}
				} 
				item.isRight = this.$cookies.get('isRightt_' + (($this.currentPage - 1) * $this.pageSize + i));
			});
		},
		//计算题目数量
		countquestiontype(){
			this.testpaperForm.danxuan=0
			this.testpaperForm.duoxuan=0
			for(item in this.achievementForm.achievement){
				if(this.achievementForm.achievement[item].type=="单选"){
					this.testpaperForm.danxuan+=1
					
				}
				else if(this.achievementForm.achievement[item].type=="多选"){
					this.testpaperForm.duoxuan+=1
					
				}
			}
		
			this.testpaperForm.danxuan=(this.testpaperForm.danxuan).toString()
			this.testpaperForm.duoxuan=(this.testpaperForm.duoxuan).toString()
		
		},
		testmessage(){
			this.achievementForm.score =parseInt(this.achievementForm.score)
			this.achievementForm.passMark =parseInt(this.achievementForm.passMark)
			if (this.achievementForm.score >= this.achievementForm.passMark) {
			this.$toast("考试通过");
			} 
			if (this.achievementForm.score < this.achievementForm.passMark&& this.achievementForm.testNumber <= this.testTime) {
				this.$toast("考试未通过，请点击重做");
				this.resetBtn = true
			}
			if (this.achievementForm.score < this.achievementForm.passMark && this.achievementForm.testNumber > this.testTime) {
				this.$toast("考试"+this.testTime+"次未通过，不能重做了");
				this.resetBtn = false
			}
		},
		testcheckmessage(){
			if (this.achievementForm.score != "" && this.achievementForm.score != null) {
				if (parseInt(this.achievementForm.score) >= parseInt(this.achievementForm.passMark)) {
					this.$toast("您已经通过考试，无需再考");
				} else if (parseInt(this.achievementForm.score) < parseInt(this.achievementForm.passMark) && parseInt(this.achievementForm
					.testNumber) < this.testTime) {
					this.$toast("您未通过考试，请重做");
					this.resetonBtn = true;
					this.hidetimu = true
				}
				this.nextDisable = true;
				this.beforeDisable = true;
				this.answerDisable = true;
			}
			
			this.testpaperForm = this.achievementForm
			console.log(this.testpaperForm)
			this.testpaperForm.testNumber = parseInt(this.testpaperForm.testNumber) + 1
			if (this.achievementForm.testNumber > this.testTime && this.achievementForm.score != "" && this.achievementForm.score !=
				null &&parseInt(this.achievementForm.score) < parseInt(this.achievementForm.passMark)) {
					this.comfirm = true
				this.$toast("您"+this.testTime+"次未通过，不能重做了！");
				this.resetBtn = false;
				this.resetonBtn = false;
			}
			this.testpaperForm.testNumber = this.testpaperForm.testNumber.toString()
		},
		async submit() {
			for (let item in this.achievementForm.achievement) {
				if (this.achievementForm.achievement[item].isRight == true) {
					this.right += 1
				}
			}
			this.onegoal = this.achievementForm.totalScore / this.achievementForm.questionTotal
			this.achievementForm.score = this.onegoal * this.right
			this.achievementForm.score = this.achievementForm.score.toString()
			let res = await axios.put('api/testpaperview/save', this.achievementForm);
			try {
				this.submit =true
				if (res.code === 200) {
					this.comfirm = true
					this.showrightchoose = true
					this.show_right_answer = true
					if (this.achievementForm.score < this.achievementForm.passMark) {
						if (this.achievementForm.score < this.achievementForm.passMark && this.achievementForm.testNumber > this.testTime) {
							this.$toast("考试"+this.testTime+"次未通过，不能重做了");
							this.resetBtn = false
						} else {
							this.reform();
						}
					}
				} else {
					this.$toast("后端异常");
				}
			} catch (e) {
				this.$toast("系统异常");
			}
			this.testmessage();
		},
		async reform() {
			this.remove()
			this.countquestiontype()
			console.log(this.testpaperForm)
			//var testid;
			try {
				let res = await axios.put('api/testpaperview/addpaper', this.testpaperForm);
				if (res.code === 200) {
					this.$toast("本次考试未通过，请重考。");
					this.testid = res.data[0]
					//window.location.href = "examination.html?testid=" + testid
				} else {
					this.$toast(res.message);
				}
			} catch (e) {
				this.$toast("系统异常");
			}
			this.createpaperDisable = false;
			

		},
		relink() {
			window.location.href = "p_testpaper.html"
		},
		async readquestion() {
			try {
				this.remove()
				let res = await axios.post('api/testpaperview/read', this.achievementForm);
				if (res.code === 200) {
					this.Form = deepCopy(res.data[0].recs)
					this.achievementForm =deepCopy(res.data[0])
					//this.achievementForm.stestType = res.data[0].recs[0].stestType
					// this.number = res.data[0].recs.length
				} else if(res.code === 201){
					this.$toast("不可生成试卷");
					this.resetonBtn =false;
				}else {
					this.$toast("后端错误");
				}
			} catch (e) {
				this.$toast("系统错误");
			}
			if(this.achievementForm.examId==0){
				this.testTime = 3
			}else if(this.achievementForm.examId!=0){
				this.testTime = parseInt(this.achievementForm.testTotalTimes)
			}
			this.testcheckmessage()

			for (let item in this.Form) {
				//需要判断this.Form[item].xuan_xiang不为空

				this.Form[item].rightchoose = this.Form[item].rightchoose.replace(/'/g, '')
				this.Form[item].rightchoose = this.Form[item].rightchoose.split(',')
				this.Form[item].pickedMany = []
				this.Form[item].picked_radio = ""
				this.Form[item].text = ""
				this.Form[item].type = this.Form[item].questionType
				for(let i in this.Form[item].xuanXiang.length){
					this.Form[item].xuanXiang[i] =this.Form[item].xuanXiang[i].text
				}

			}
			this.achievementForm.achievement = this.Form
		

			if (parseInt((this.achievementForm.achievement.length + this.pageSize - 1) / this.pageSize) <= 1) {
				this.nextDisable = true
			}
			

			this.countquestiontype()


			this.init();
		},
		doCheck(value,item, index) {

			if (item.type == "单选") {
				//单选
				this.achievementForm.achievement[index].answerResults = item.picked_radio

				if (item.picked_radio == item.rightchoose[0]) {
					item.isRight = true;
				} else {
					item.isRight = false;
				}

			} else if (item.type == "多选") {
				//多选 
				var arr = []
		
				for (i in item.pickedMany) {
					arr.push(item.pickedMany[i])
				}
			
				this.achievementForm.achievement[index].answerResults = item.pickedMany
				if (JSON.stringify(arr.sort()) == JSON.stringify(item.rightchoose.sort())) {
					item.isRight = true;
				} else {
					item.isRight = false;
				}
			} 
			// 将结果放入缓存
			if (item.type == "单选") {
				this.$cookies.set('pickedd_' + index, item.picked_radio);

			} else if (item.type == "多选") {
				this.$cookies.set('pickedd_' + index, JSON.stringify(item.pickedMany));
			}

			this.$cookies.set('isRightt_' + index, item.isRight);
			this.$cookies.set('isChose_' + index, true);

		},
		remove() {
			for (item in this.$cookies.keys()) {
				this.$cookies.remove(this.$cookies.keys()[item])
			}

		},
		prev() { //上一页

			if (this.currentPage > 1) {
				this.currentPage = this.currentPage - 1;
				this.nextDisable = false
				this.init();
			}
			if (this.currentPage == 1) {
				this.nextDisable = false
				this.beforeDisable = true
				
			}
		},
		next() { //下一页

			if (this.currentPage < this.allPageNum) {
				this.currentPage = this.currentPage + 1;
				this.beforeDisable = false
				this.init();

			}
			if (this.currentPage == this.allPageNum) {
				//this.submitDisable = false
				this.nextDisable = true
		
			}

		},

		detail() { //答题状况
			var $this = this;
			this.achievementForm.achievement.forEach(function(item, i) {
				item.isRight = JSON.parse($this.$cookies.get('isRightt_' + i));
				item.isChose = JSON.parse($this.$cookies.get('isChose_' + i));
			});
		}
	},
	computed: {
		allPageNum: {
			get: function() {
				return parseInt((this.achievementForm.achievement.length + this.pageSize - 1) / this.pageSize);
			},
		}
	},

	mounted() {
		this.readquestion();

	},
	created() {
		var url = location.search;

		strs = url.split("=");
		this.achievementForm.testId = strs[1] == undefined ? this.achievementForm.testId : strs[1];
		this.testpaperForm.testId = strs[1] == undefined ? this.testpaperForm.testId : strs[1];

	}
});
